/**
 * Created by MLS on 15/11/13.
 */
(function($){
    var pageTabel = $('#pageMessage').DataTable({
        "ajax": "index/searchLog",
        "paging": true,
        "searching": false,
        "lengthChange": false,
        "info": true,
        "pageLength":10,
        //"lengthChange":true,
        "autoWidth": false,
        "scrollX": true,
        "order": [],
        "columns":[
            {"data":"appName"},
            {"data":"operationType"},
            {"data":"request"},
            {"data":"remark"},
            {"data":"version"},
            {"data":"createUser"},
            {"data":"createTime"}
        ],
        "columnDefs": [
            {
                "targets": [0],
                "data": "description",
                "render": function ( data, type, full, meta ) {
                    return '<a class="label label-default" information >'+data+'</a>';
                }
            },
            {
            "targets": [2],
            "data": "description",
            "render": function ( data, type, full, meta ) {
                    var data = $.parseJSON(data);

                    var serverList = null;
                    if(data){
                        serverList = data.serverProcessInfoListList;
                    }
                    var span = "";
                    if(serverList!=null && serverList.length>0){
                        for(var i=0;i<serverList.length;i++){
                            var processObjList = serverList[i].processList;
                            var processEs = [];
                            $(processObjList).each(function(){
                                processEs.push(this.moduleName);
                            });
                            span += '<span data-trigger="hover" data-html="true" class="label label-info" data-toggle="popover" title="服务信息" data-content="'+processEs.join("<br/>")+'">'+serverList[i].ip+'</span>';
                        }
                    }
                    return span;
            }
        }
            ,
            {
                "targets": [4],
                "data": "description",
                "render": function ( data, type, full, meta ) {
                    if(!data){return "";}
                    return '<div class="btn-group"><button class="btn btn-default btn-xs dropdown-toggle btn-warning" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">'+
                    data+'<span class="caret"></span>'+
                    '</button>'+
                    '<ul class="dropdown-menu">'+
                    '<li><a href="#" version="'+data+'">还原</a></li>'+
                    ' </ul></div>';
                }
            }

        ],
        "createdRow": function( row, data, dataIndex ) {
            if ( !data.suc) {
                $(row).addClass( 'danger' );
            }
            $(row).find("[information]").on("click",function(){showReqMess(data);});
        },
        "drawCallback": function( settings ) {
            $('[data-toggle="popover"]').popover()
            $("[version]").on("click",restore);
        }
    });

    function showReqMess(obj){
        $("#requestMessage").html(obj.request);
        $("#responseMessage").html(obj.response);
        $("#costTime").html(obj.costTime);
        $("#requestIp").html(obj.ip);
        $('#operateMessage').modal();
    }

    function restore(obj){
        var version = $(obj.currentTarget).attr("version");
        window.Dialog.prompt({titel:"添加备注信息",func:function(remark){
            restAjax("index/reStore",window.RequestMethod.GET,{
                data:{"version":version,remark:remark},
                success:function(data){
                    if(!data.suc){
                        window.Dialog.alert(data.data.errorContent);
                    }else{
                        pageTabel.ajax.reload();
                    }
                }
            });
        }});

    }
})(jQuery);